<template>
  <div class="pd-20">
    <el-row :gutter="20">
      <el-col
        :span="20"
        v-if="globalConfig && globalConfig.demon_mode"
        style="margin-left: 120px; margin-top: 100px"
      >
        <el-card shadow="hover" header="欢迎" class="mb-20">
          <div class="welcome">
            <div class="logo">
              <img src="@/assets/img/logo.png" />
              <h2>欢迎体验 {{ $packageData.name }}</h2>
            </div>
            <div class="tips">
              <div
                class="tips-item"
                v-for="item in $packageData.funcList"
                :key="item.icon"
              >
                <div class="tips-item-icon"><i :class="item.icon"></i></div>
                <div class="tips-item-message" v-text="item.text"></div>
              </div>
            </div>
            <div class="actions">
              <router-link to="/chat">
                <el-button
                  type="primary"
                  icon="el-icon-s-promotion"
                  size="large"
                  >去聊天</el-button
                >
              </router-link>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  components: {},
  computed: {
    ...mapState({
      globalConfig: (state) => state.globalConfig,
    }),
  },
  data() {
    return {
      loading: false,
      taskStatus: false,
      taskList: [],
      curName: "",
      dialogTableVisible: false,
      taskLog: "",
      task: [
        {
          name: "im_task_schedule",
          started: "--",
          status: "stop",
          remark: "计划任务",
        },
        {
          name: "im_task_queue",
          started: "--",
          status: "stop",
          remark: "消息队列",
        },
        {
          name: "im_task_worker",
          started: "--",
          status: "stop",
          remark: "消息推送",
        },
      ],
    };
  },
  mounted() {
    this.resetTask();
    this.getTaskList();
  },
  methods: {
    resetTask() {
      let task = this.task;
      this.taskList = task;
    },
    getTaskList() {
      this.$api.taskApi.getTaskList().then((res) => {
        if (res.code == 400) {
          this.taskStatus = false;
        } else if (res.code == 0) {
          this.taskStatus = true;
          this.taskList = res.data;
        }
      });
    },
    startService() {
      this.loading = true;
      if (this.taskStatus == false) {
        this.$api.taskApi.startTask().then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.getTaskList();
          }
        });
      } else {
        this.$confirm("确定要停止服务吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$api.taskApi.stopTask().then((res) => {
              this.loading = false;
              if (res.code == 0) {
                this.taskStatus = false;
                this.resetTask();
              }
            });
          })
          .catch(() => {
            this.loading = false;
            this.$message({
              type: "info",
              message: "已取消停止",
            });
          });
      }
    },
    // 获取进程日志
    showLog(name) {
      this.curName = name;
      this.$api.taskApi.getTaskLog({ name: name }).then((res) => {
        if (res.code == 0) {
          if (res.data == "") return this.$message.error("暂无日志");
          this.dialogTableVisible = true;
          this.taskLog = res.data;
        }
      });
    },
    clearTaskLog() {
      this.$confirm("确定要清除日志吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api.taskApi.clearTaskLog({ name: this.curName }).then((res) => {
            if (res.code == 0) {
              this.dialogTableVisible = false;
              this.taskLog = "";
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped lang="scss">
.item-background p {
  color: #999;
  line-height: 1.8;
}

.welcome {
}

.welcome .logo {
  text-align: center;
}

.welcome .logo img {
  vertical-align: bottom;
  width: 80px;
  height: 80px;
  margin-bottom: 20px;
}

.welcome .logo h2 {
  font-size: 24px;
  font-weight: normal;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tips {
  margin-top: 20px;
}

.tips-item {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 10px;
}

.tips-item-icon {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  font-size: 18px;
  margin-right: 20px;
  color: var(--el-color-primary);
  background: rgba(180, 180, 180, 0.1);
}

.tips-item-message {
  flex: 1;
  font-size: 14px;
}

.actions {
  text-align: center;
  margin: 20px 0 20px 0;
}
.stop-task:hover {
  color: #f56c6c;
}
.start-task {
  color: #409eff;
}
.task-name {
  font-weight: 600;
}

.task-log {
  background: #000;
  color: #fff;
  white-space: break-spaces;
  letter-spacing: 1px;
}
</style>
